package com.dmgis.hndz.common.config.primary;//package com.dmgis.hndz.common.config.primary;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
@EnableMongoRepositories(basePackages = "com.dmgis.hndz.common.config.primary", mongoTemplateRef = "mongoPrimaryTemplate")
public class MongoPrimaryTemplateConfig {
    @Value("${spring.data.mongodb.primary.uri}")
    private String uri;

    @Primary
    @Bean(name = "mongoPrimaryTemplate")
    public MongoTemplate mongoMpcTemplate() throws Exception {
        return new MongoTemplate(mongoMpcFactory());
    }

    @Bean
    @Primary
    public MongoDatabaseFactory mongoMpcFactory() throws Exception {
        SimpleMongoClientDatabaseFactory simpleMongoClientDbFactory = new SimpleMongoClientDatabaseFactory(uri);
        return simpleMongoClientDbFactory;
    }

}
